<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:lzc="http://schemas.icitadel.com/Lazarus/Compiled.xsd" version="1.0">

  <xsl:output method="text"/>

  <xsl:include href="../../common.xsl" />
  <xsl:include href="../../convert.xsl" />

  <xsl:template match="lzc:project">
    <xsl:apply-templates select="lzc:datasource" />
  </xsl:template>

  <xsl:template match="lzc:datasource">#!/bin/bash

echo '<xsl:value-of select="ancestor::lzc:project/@name" />'
echo '-------------------------------------------------------------------------'
echo 'create_schema.sh'
echo 'Creates the Google Cloud SQL Database on the specified instance.'
echo 'Requires an alias for gsql to your google_sql.sh script.'
echo '  eg: alias gsql=''sh /usr/bin/google_sql/google.sql.sh'''
echo ' '
echo ' '
echo 'Syntax: crebas.sh <xsl:value-of select="concat(@location, ' ', @schema)" />'
echo ' '

DEFAULT_INSTANCE='<xsl:value-of select="@location" />'
DEFAULT_DATABASE='<xsl:value-of select="@schema" />'

CheckInstance ()
{
  echo ' '
  echo 'Checking for specified Instance Name.'
  if [ $1 ]; then
    instance_name=$1
    echo 'Using default Instance '$1
  else
    instance_name=$DEFAULT_INSTANCE
  fi
}

CheckDatabase ()
{
  echo ' '
  echo 'Checking for specified Database.'
  if [ $2 ]; then
    database_name=$2
    echo 'Using default Database '$2
  else
    database_name=$DEFAULT_DATABASE
  fi
}

google_sql='sh /usr/local/google_sql_tool/google_sql.sh'

CheckInstance
CheckDatabase

echo ' '
echo 'Running Scripts...'
echo ' '
echo '*** Step 1: Creating Tables, Columns and Defaults.'
$google_sql $instance_name $database_name &lt; create_tables.sql
echo ' '
echo '*** Step 2: Adding References (Foreign Keys)'
$google_sql $instance_name $database_name &lt; create_references.sql
echo ' '
echo '*** Step 3: Adding Indexes (References and Explicit Indexes)'
$google_sql $instance_name $database_name &lt; create_indexes.sql

echo ' '
echo '*** Step 4: Creating Stored Procedures'
pushd procs
sh create_procs.sh $instance_name $database_name
echo ' '

popd

echo ' '
echo 'Database Generation Complete'
  </xsl:template>
</xsl:stylesheet>
